package com.aliyun.oss.controller;

import com.aliyun.oss.pojo.DTO.OrderSearchFormDTO;
import com.aliyun.oss.pojo.Entity.Orders;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.aliyun.oss.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    //    获取所有订单
    @GetMapping("/list")
    public ResultVO list(Integer pageNumber) {
        System.out.println("进入订单列表"+pageNumber);
//        要求按照时间进行倒叙
        Page<Orders> page = new Page<>(pageNumber, 10);
//        return ResultVO.success(orderService.list(new QueryWrapper<Orders>().orderByDesc("order_time")));
        return ResultVO.success(orderService.page(page, new QueryWrapper<Orders>().orderByDesc("order_time")));
    }

    //    根据订单状态查询订单
    @GetMapping("/listByStatus/{status}/{pageNumber}")
    public ResultVO listByStatus(@PathVariable Integer status,@PathVariable Integer pageNumber) {
        Page<Orders> page = new Page<>(pageNumber, 10);

        return ResultVO.success(orderService.page(page,new QueryWrapper<Orders>().eq(status != 0, "status", status).orderByDesc("order_time")));
    }

    //    查询订单
    @GetMapping("/searchOrders")
    public ResultVO searchOrders(OrderSearchFormDTO orderSearchFormDTO) {
        LocalDateTime startOfDay=null;
        LocalDateTime endOfDay=null;
        Page<Orders> page = new Page<>(orderSearchFormDTO.getPageNumber(), 10);
        if (orderSearchFormDTO.getStartOrderTime() != null) {
            startOfDay = orderSearchFormDTO.getStartOrderTime().atStartOfDay();
        }
        if (orderSearchFormDTO.getEndOrderTime() != null) {
            endOfDay = orderSearchFormDTO.getEndOrderTime().atTime(23, 59, 59);
        }
//        return ResultVO.success(orderService.list(new QueryWrapper<Orders>().like(orderSearchFormDTO.getNumber() != null, "number", orderSearchFormDTO.getNumber())
//                .like(orderSearchFormDTO.getPhone() != null, "phone", orderSearchFormDTO.getPhone())
//                .ge(orderSearchFormDTO.getStartOrderTime() != null, "order_time", startOfDay)
//                .le(orderSearchFormDTO.getEndOrderTime() != null, "order_time", endOfDay)
//                .eq(orderSearchFormDTO.getStatus() != 0, "status", orderSearchFormDTO.getStatus())
//        ));
        return ResultVO.success(orderService.page(page,new QueryWrapper<Orders>().like(orderSearchFormDTO.getNumber() != null, "number", orderSearchFormDTO.getNumber())
                .like(orderSearchFormDTO.getPhone() != null, "phone", orderSearchFormDTO.getPhone())
                .ge(orderSearchFormDTO.getStartOrderTime() != null, "order_time", startOfDay)
              .le(orderSearchFormDTO.getEndOrderTime() != null, "order_time", endOfDay)
                .eq(orderSearchFormDTO.getStatus() != 0, "status", orderSearchFormDTO.getStatus())));
    }
//    根据订单id修改订单状态
    @PutMapping("/updateStatus/{id}/{status}")
    public ResultVO updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        orderService.updateById(new Orders(id, status));
        return ResultVO.success();
    }
}
